package com.shaw.mybatisx.executor.result;

import com.shaw.running.pojo.User;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by asus on 2018/4/8.
 */
public class ResultSetHandler {
    public <E> List<E> handlerResultSet(ResultSet resultSet, Class<E> returnType){
        List<E> resultList = new ArrayList<E>();
        try {
            Field[] fields = returnType.getDeclaredFields();
            while (resultSet.next()){
                E returnObj = returnType.newInstance();
                for(Field field:fields){
                    String methodName = "set" + field.getName().substring(0,1).toUpperCase()+field.getName().substring(1);
                    Method method = returnType.getDeclaredMethod(methodName,field.getType());
                    method.invoke(returnObj,getColValue(resultSet,field.getName(),field.getType()));
                }
//                user.setId(rs.getInt("id"));
//                user.setUsername(rs.getString("username"));
                resultList.add(returnObj);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return resultList;
    }

    private static <E> E getColValue(ResultSet resultSet,String colName,E fieldType) throws SQLException {
        if(Integer.class==fieldType){
            return (E)Integer.valueOf(resultSet.getInt(colName));
        }else if(String.class == fieldType){
            return (E)resultSet.getString(colName);
        }else if(Short.class == fieldType){
            return (E)Short.valueOf(resultSet.getShort(colName));
        }else if(Long.class == fieldType){
            return (E)Long.valueOf(resultSet.getLong(colName));
        }else if(Byte.class == fieldType){
            return (E)Long.valueOf(resultSet.getByte(colName));
        }else if(Double.class == fieldType){
            return (E)Double.valueOf(resultSet.getDouble(colName));
        }else if(Float.class == fieldType){
            return (E)Float.valueOf(resultSet.getFloat(colName));
        }else if(Boolean.class == fieldType){
            return (E)Boolean.valueOf(resultSet.getBoolean(colName));
        }else{
            return null;
        }
    }

    public static void main(String[] args) throws NoSuchMethodException {
        Class clazz = User.class;
        Field[] fields = clazz.getDeclaredFields();
        for(Field field:fields){
            String methodName = "set" + field.getName().substring(0,1).toUpperCase()+field.getName().substring(1);
            Method method = clazz.getDeclaredMethod(methodName,field.getType());
            System.out.println(method.getName());
        }

    }
}
